# coding=utf-8
"""
Model库的基本模型
"""
import datetime
#from sqlalchemy import Column, BigInteger, ForeignKey, DateTime, String
from sqlalchemy import Column, BigInteger, DateTime, String
#from sqlalchemy.orm import relationship, backref
from sqlalchemy.sql.expression import text
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.ext.hybrid import hybrid_method
from project.tools import JsonpickleMixin

class BaseModel(JsonpickleMixin):
    """
    model的基本实现
    """
    id = Column('id', BigInteger, primary_key=True)

    @declared_attr
    def insert_user(cls):
        """
        新建用户
        """
        return Column('insert_user', String)

    @declared_attr
    def insert_time(cls):
        """
        新建时间
        """
        return Column('insert_time', DateTime, default=datetime.datetime.now,
            onupdate=datetime.datetime.now, server_default=text('NOW()'))

    @declared_attr
    def update_user(cls):
        """
        更新用户
        """
        return Column('update_user', String)

    @declared_attr
    def update_time(cls):
        """
        更新时间
        """
        return Column('update_time', DateTime, default=datetime.datetime.now,
        onupdate=datetime.datetime.now, server_default=text('NOW()'))

    @hybrid_method
    def set_insert_user(self, user):
        """
        同步更新user
        """
        self.insert_user = user
        self.update_user = user

    @hybrid_method
    def set_update_user(self, user):
        """
        同步更新user
        """
        self.update_user = user
